package com.hubujun.driverlicense.util;

import android.app.AlertDialog;
import android.app.Dialog;
import android.content.Context;
import android.content.DialogInterface;
import android.os.Looper;
import android.view.WindowManager;


import com.hubujun.driverlicense.ApplicationContext;

import java.lang.Thread.UncaughtExceptionHandler;

public class MyCrashHandler implements UncaughtExceptionHandler {

	private static MyCrashHandler mInstance = null;

	public static MyCrashHandler getInstance() {
		if (mInstance == null) {
			synchronized (MyCrashHandler.class) {
				if (mInstance == null) {
					mInstance = new MyCrashHandler();
				}
			}
		}
		return mInstance;
	}

	@Override
	public void uncaughtException(final Thread thread, final Throwable ex) {
		new Thread() {
			@Override
			public void run() {
				Looper.prepare();
				Ln.e(ex, "thread.name %s, thread.id %s, uncaughtException.", thread.getName(), thread.getId());
				Context context = ApplicationContext.getContext();
				Dialog dialog = new AlertDialog.Builder(context).setTitle("温馨提示").setMessage("荔枝FM出现bug，日志文件已经保存，请联系开发人员！")
						.setPositiveButton("确定", new DialogInterface.OnClickListener() {
							@Override
							public void onClick(DialogInterface dialog, int which) {
								android.os.Process.killProcess(android.os.Process.myPid());
							}
						}).create();
				dialog.getWindow().setType(WindowManager.LayoutParams.TYPE_SYSTEM_ALERT);
				dialog.show();
				Looper.loop();
			}
		}.start();

	}
}
